package vn.com.vega.chacha.v2;

import java.util.ArrayList;
import java.util.List;

import vn.com.vega.chacha.R;
import vn.com.vega.chacha.v2.Asynctask.BaseAsyncTask;
import vn.com.vega.music.database.DataStore;
import vn.com.vega.music.objects.Song;
import vn.com.vega.music.objects.Video;
import vn.com.vega.music.view.adapter.SongListAdapter;
import vn.com.vega.music.view.adapter.VideoListAdapter;
import vn.com.vega.music.view.custom.BottomPlayerView;
import vn.com.vega.music.view.custom.BottomPlayerView.ShowHideBottomPlayerListener;
import vn.com.vega.music.view.custom.LoadingView;
import vn.com.vega.music.view.custom.LoadingView.RetryListener;
import vn.com.vega.music.view.custom.TopTaskbarView;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

public class DownloadedScreen extends ActivityBase implements OnClickListener {
	private TopTaskbarView mTopTaskbarView;
	private ListView mListView;
	private SongListAdapter mSongListAdapter;
	private VideoListAdapter mVideoListAdapter;
	private DataStore dataStore;
	private LoadingView mLoadingView;
	private BottomPlayerView mBottomPlayer;
	private View mPlayerViewReplace;
	private View mPlayAllSong;
	private View mSelectSongPlay;
	private View mHeaderListSong;
	
	private View mFillterView;
	private TextView mFillterLabel;
	private ArrayList<String> fillterListString = new ArrayList<String>();
	private static final String DIALOG_FILLTER_LIKED = "dialog fillter liked";
	
	private static final int mIndexSongDownloaded = 0;
	private static final int mIndexVideoDownloaded = 1;
	private int mIndexCurrentListShow = mIndexSongDownloaded;
	
	private List<Object> listSongDownloaded;
	private List<Object> listVideoDownloaded;

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		dataStore = DataStore.getInstance();
//		if (dataStore.getListDownloadedSongs().size() == 0) {
//			showToast("Chưa tải về bài hát nào!");
//			finish();
//			return;
//		}

		setContentView(R.layout.downloaded_screen);
		mListView = (ListView) findViewById(R.id.lv_list_song);
		mLoadingView = (LoadingView) findViewById(R.id.loadingView_loading_list_song_local);
		mLoadingView.setVisibility(View.GONE);
		mTopTaskbarView = (TopTaskbarView) findViewById(R.id.topTaskbarView_song_local);
		mTopTaskbarView.showBackButton();
		mTopTaskbarView.changeToTitleOnly("Nhạc đã tải về");
//		List<Object> list = new ArrayList<Object>();
//		list.addAll(dataStore.getListDownloadedSongs());
//		mSongListAdapter = new SongListAdapter(this, list, true,
//				DownloadedScreen.class.getSimpleName());
//		mListView.setAdapter(mSongListAdapter);
		
		mSelectSongPlay = findViewById(R.id.tv_select_song_play);
		mPlayAllSong = findViewById(R.id.tv_play_all_song);
		mSelectSongPlay.setOnClickListener(this);
		mPlayAllSong.setOnClickListener(this);
		mHeaderListSong = findViewById(R.id.layout_header_list_song);

		mPlayerViewReplace = findViewById(R.id.view_replace_bottom_player);
		mBottomPlayer = (BottomPlayerView) findViewById(R.id.bottomPlayerView_song_local);
		mBottomPlayer.setShowHideListener(showHideBottomPlayerListener);
		
		mFillterView = findViewById(R.id.layout_liked_fillter);
		mFillterLabel = (TextView) findViewById(R.id.tv_liked_fillter);

		fillterListString.add(getString(R.string.fillter_music));
		fillterListString.add(getString(R.string.fillter_video));

		mFillterLabel.setText(getString(R.string.fillter_music));
		mFillterView.setOnClickListener(selectFillterClickListener);

		mLoadingView.setRetryListener(retryListener);
		loadContent();
	}
	
	RetryListener retryListener = new RetryListener() {
		
		@Override
		public void onRetry() {
			// TODO Auto-generated method stub
			loadContent();
		}
	};
	
	private void loadContent() {
		if (loadListContentDownloaded != null) {
			loadListContentDownloaded.cancel(true);
		}
		switch (mIndexCurrentListShow) {
		case mIndexSongDownloaded:
			loadListContentDownloaded = new LoadListContentDownloaded(this, LoadListContentDownloaded.TYPE_SONG);
			loadListContentDownloaded.execute();
			break;
		case mIndexVideoDownloaded:
			loadListContentDownloaded = new LoadListContentDownloaded(this, LoadListContentDownloaded.TYPE_VIDEO);
			loadListContentDownloaded.execute();
			break;

		default:
			break;
		}
	}
	
	View.OnClickListener selectFillterClickListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			showFilltter(fillterListString, selectListDownloadedListener,
					DIALOG_FILLTER_LIKED);
		}
	};
	
	AdapterView.OnItemClickListener selectListDownloadedListener = new AdapterView.OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int position,
				long arg3) {
			// TODO Auto-generated method stub
			hideDialogFillter(DIALOG_FILLTER_LIKED);
			changeListDownloaded(position);
		}
	};
	
	private void changeListDownloaded(int position) {
		if (position != mIndexCurrentListShow) {
			mIndexCurrentListShow = position;
			switch (mIndexCurrentListShow) {
			case mIndexSongDownloaded:
				mHeaderListSong.setVisibility(View.VISIBLE);
				mFillterLabel.setText(getString(R.string.fillter_music));
				mTopTaskbarView.changeToTitleOnly("Nhạc tải về máy");
				if (mSongListAdapter != null && mSongListAdapter.getCount() > 0) {
					mLoadingView.loadingFinish(true, null);
					mListView.setAdapter(mSongListAdapter);
				} else {
					loadContent();
				}
				break;
			case mIndexVideoDownloaded:
				mHeaderListSong.setVisibility(View.GONE);
				mFillterLabel.setText(getString(R.string.fillter_video));
				mTopTaskbarView.changeToTitleOnly("Video tải về máy");
				if (mVideoListAdapter != null && mVideoListAdapter.getCount() > 0) {
					mLoadingView.loadingFinish(true, null);
					mListView.setAdapter(mVideoListAdapter);
				} else {
					loadContent();
				}
				break;

			default:
				break;
			}
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		mBottomPlayer.startUpdate();
	}

	@Override
	protected void onPause() {
		super.onPause();
		mBottomPlayer.stopUpdate();
	}
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.tv_select_song_play:
			if (mSongListAdapter != null && mSongListAdapter.getCount() > 0) {
//				Appinstant app = (Appinstant) getApplication();
//				app.tranportString.put(SelecSongPlay.KEY_LIST_SONG_TITLE, "List song downloaded");
				mSongListAdapter.selectSongPlay("Các bài hát tải về");
			}
			break;
		case R.id.tv_play_all_song:
			if (mSongListAdapter != null && mSongListAdapter.getCount() > 0) {
				mSongListAdapter.playAllSong("Song downloaded");
			}
			break;

		default:
			break;
		}
	}

	ShowHideBottomPlayerListener showHideBottomPlayerListener = new ShowHideBottomPlayerListener() {

		@Override
		public void onShowBottomPlayer() {
			// TODO Auto-generated method stub
			mPlayerViewReplace.setVisibility(View.VISIBLE);
		}

		@Override
		public void onHideBottomPlayer() {
			// TODO Auto-generated method stub
			mPlayerViewReplace.setVisibility(View.GONE);
		}
	};
	
	private void showListContent(int indexListConten) {
		switch (indexListConten) {
		case mIndexSongDownloaded:
			if (listSongDownloaded == null) {
				mLoadingView.loadingFinish(false, null);
			} else if (listSongDownloaded.size() == 0) {
				mLoadingView.loadingFinish(false, "Chưa tải về bài hát nào!");
			} else {
				mLoadingView.loadingFinish(true, null);
				mSongListAdapter = new SongListAdapter(this, listSongDownloaded, false, DownloadedScreen.class.getName());
				mListView.setAdapter(mSongListAdapter);
			}
			break;
		case mIndexVideoDownloaded:
			if (listVideoDownloaded == null) {
				mLoadingView.loadingFinish(false, null);
			} else if (listVideoDownloaded.size() == 0) {
				mLoadingView.loadingFinish(false, "Chưa tải về video nào!");
			} else {
				mLoadingView.loadingFinish(true, null);
				mVideoListAdapter = new VideoListAdapter(this, listVideoDownloaded, false);
				mListView.setAdapter(mVideoListAdapter);
			}
			
			break;

		default:
			break;
		}
	}
	
	private LoadListContentDownloaded loadListContentDownloaded;
	
	static class LoadListContentDownloaded extends BaseAsyncTask {
		public static final int TYPE_VIDEO = 1;
		public static final int TYPE_SONG = 0;
		
		private int typeLoad = TYPE_SONG;
		private List<Object> listContent;

		public LoadListContentDownloaded(DownloadedScreen base, int typeContent) {
			super(base);
			// TODO Auto-generated constructor stub
			typeLoad = typeContent;
		}

		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			if (!checkIsLive() || isCancelled()) {
				return;
			}
			DownloadedScreen downloadedScreen = (DownloadedScreen) baseActivityRfc.get();
			switch (typeLoad) {
			case TYPE_SONG:
				downloadedScreen.listSongDownloaded = listContent;
				downloadedScreen.showListContent(mIndexSongDownloaded);
				break;
			case TYPE_VIDEO:
				downloadedScreen.listVideoDownloaded = listContent;
				downloadedScreen.showListContent(mIndexVideoDownloaded);
				break;
			default:
				break;
			}
		}

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			DownloadedScreen downloadedScreen = (DownloadedScreen) baseActivityRfc.get();
			downloadedScreen.mLoadingView.startLoading();
		}

		@Override
		protected Void doInBackground(Void... params) {
			// TODO Auto-generated method stub
			DownloadedScreen downloadedScreen = (DownloadedScreen) baseActivityRfc.get();
			switch (typeLoad) {
			case TYPE_SONG:
				List<Song> listSong = downloadedScreen.dataStore.getListDownloadedSongs();
				if (listSong != null) {
					listContent = new ArrayList<Object>();
					listContent.addAll(listSong);
				}
				break;
			case TYPE_VIDEO:
				List<Video> listVideo = downloadedScreen.dataStore.getListVideoDownloaded();
				if (listVideo != null) {
					listContent = new ArrayList<Object>();
					listContent.addAll(listVideo);
				}
				break;

			default:
				break;
			}
			
			return null;
		}
		
	}

}
